package io.renren.modules.offer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.advertiser.entity.AdvertiserEntity;
import io.renren.modules.offer.dao.OfferDao;
import io.renren.modules.offer.entity.OfferEntity;
import io.renren.modules.offer.service.OfferService;
import io.renren.modules.offer.service.OfferUtilService;
import io.renren.modules.util.service.SysSwitchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service("offerService")
public class OfferServiceImpl extends ServiceImpl<OfferDao, OfferEntity> implements OfferService {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(OfferServiceImpl.class);

    @Autowired
    private OfferUtilService offerUtilService;

    @Autowired
    private SysSwitchService sysSwitchService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<OfferEntity> page = this.selectPage(
                new Query<OfferEntity>(params).getPage(),
                new EntityWrapper<OfferEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void pull(List<OfferEntity> offerEntityList, String batchNo, AdvertiserEntity advertiser) {
        //将数据库中批次号全部设置为0
        baseMapper.updateOfferSetBatchNo(advertiser.getSspChannelId());

        for (OfferEntity offerEntity : offerEntityList) {
            OfferEntity dbOfferEntity = baseMapper.getOfferByDspOfferId(offerEntity.getDspOfferId(),advertiser.getSspChannelId());
            if (dbOfferEntity != null) {
                offerEntity.setId(dbOfferEntity.getId());
                offerEntity.setDspChannelId(advertiser.getDspChannelId());
                offerEntity.setSspChannelId(advertiser.getSspChannelId());
                offerEntity.setSspOfferId(dbOfferEntity.getSspOfferId());
                //比较一下传入字段与数据库中的差别，如果不同则进行更新,并设置推送状态为0，表示要重新更新下游接口
//                if (offerUtilService.compareEntity(offerEntity, dbOfferEntity)) {
//                    offerEntity.setPushFlag(0);
//                    offerEntity.setPushStatus("Active");
//                }else {
//                    offerEntity.setPushFlag(1);
//                }
                offerEntity.setPushFlag(0);
                offerEntity.setPushStatus("Active");
                offerEntity.setUpdateTime(new Date());
                this.updateById(offerEntity);
            } else {
                offerEntity.setPushFlag(0);
                offerEntity.setPushStatus("Active");
                offerEntity.setCreateTime(new Date());
                this.insert(offerEntity);
            }
        }
        Map<String, Object> queryParams = new LinkedHashMap<>();
        queryParams.put("batch_no", 0);
        queryParams.put("ssp_channel_id", advertiser.getSspChannelId());
        if(sysSwitchService.getSwitch("PUSH_ALL")){
            queryParams.put("push_status", "Active");
        }

        List<OfferEntity> offerEntityPausedList = this.selectByMap(queryParams);
        LOGGER.info("需要设置为无效状态的数据条数：{}", offerEntityPausedList.size());
        for (OfferEntity offerEntity : offerEntityPausedList) {
            offerEntity.setPushStatus("Paused");
            offerEntity.setPushFlag(0);
        }
        if(offerEntityPausedList.size()!=0){
            this.updateAllColumnBatchById(offerEntityPausedList);
        }

    }

}

